package jinyilw.swing.visuals.text;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.i18n.ICSetting;
import jinyilw.swing.i18n.IString;
import jinyilw.swing.i18n.LanguageChanger;
import jinyilw.swing.rlaf.UISettable;
import jinyilw.swing.rlaf.UITools;
import jinyilw.swing.rlaf.theme.RThemeColor;
import jinyilw.swing.rlaf.utils.RBorderFactory;

import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.plaf.basic.BasicEditorPaneUI;
import javax.swing.text.EditorKit;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class RCEditorPane extends JEditorPane implements LanguageChanger
{
	private static final long serialVersionUID = 7064090767374375406L;
	private IString iString;
	private int xAlignment = StyleConstants.ALIGN_LEFT;
	private boolean transparent = true;

	public RCEditorPane()
	{
		setEditable(false);
		setBorder(RBorderFactory.getTextBorder());
	}

	public RCEditorPane(boolean transparent)
	{
		this();
		this.transparent = transparent;
	}

	public void setTransparent(boolean b)
	{
		if (transparent != b)
		{
			transparent = b;
			if (isShowing())
				repaint();
		}
	}

	@Override
	protected EditorKit createDefaultEditorKit()
	{
		return new CenterEditorKit();
	}

	public void setText(IString iString)
	{
		this.iString = iString;
		if (iString != null)
		{
			setText(iString.getI18nString());
		}
	}

	@Override
	public void changLanguage()
	{
		if (iString != null)
			setText(iString.getI18nString());
	}

	@Override
	public void setFont(Font font)
	{
		if (font != null && attributeSet != null
				&& getUI() instanceof RCEditorPaneUI)
		{
			StyleConstants.setFontFamily(attributeSet, font.getFamily());
			StyleConstants.setFontSize(attributeSet, font.getSize());
			StyleConstants.setBold(attributeSet, font.isBold());
			document.setCharacterAttributes(0, document.getLength(),
					attributeSet, true);
			if (isShowing())
				repaint();
		}
	}

	@Override
	public void setText(String t)
	{
		super.setText(t);
		if (t != null && attributeSet != null
				&& getUI() instanceof RCEditorPaneUI)
			document.setCharacterAttributes(0, document.getLength(),
					attributeSet, true);
	}

	public void setXAlignment(int alignment)
	{
		if (alignment == xAlignment || !(getUI() instanceof RCEditorPaneUI))
			return;
		xAlignment = alignment;
		StyleConstants.setAlignment(attributeSet, xAlignment);
		document.setParagraphAttributes(0, document.getLength(), attributeSet,
				true);
		if (isShowing())
			repaint();
	}

	@Override
	public void updateUI()
	{
		setUI(new RCEditorPaneUI());
		invalidate();
	}

	SimpleAttributeSet attributeSet;
	StyledDocument document;
	class RCEditorPaneUI extends BasicEditorPaneUI implements UISettable
	{
		JEditorPane editorPane;

		@Override
		public void installUI(JComponent c)
		{
			super.installUI(c);
			editorPane = (JEditorPane) c;
			editorPane.setOpaque(false);
			editorPane.setSelectionColor(RThemeColor.darkThemeColor);
			editorPane.setSelectedTextColor(RThemeColor.selectedTextColor);
			attributeSet = new SimpleAttributeSet();
			document = (StyledDocument) editorPane.getDocument();
			StyleConstants.setFontFamily(attributeSet,
					FontTools.defaultFont.getFontName());
			StyleConstants.setFontSize(attributeSet,
					FontTools.defaultFont.getSize());
			StyleConstants.setBold(attributeSet,
					FontTools.defaultFont.isBold());
			UITools.installUITheme(this);
		}

		@Override
		public void update(Graphics g, JComponent c)
		{
			if (editorPane.getSelectionColor() != RThemeColor.darkThemeColor)
				editorPane.setSelectionColor(RThemeColor.darkThemeColor);
			Composite composite = ComponentTools
					.applyEnableAlpha((Graphics2D) g, c.isEnabled(), 1f, 0.4f);
			if (!transparent)
			{
				g.setColor(RThemeColor.colorSet.transTBGColor);
				g.fillRect(2, 2, c.getWidth() - 4, c.getHeight() - 4);
			}
			paint(g, c);
			if (composite != null)
				((Graphics2D) g).setComposite(composite);
		}

		@Override
		protected void paintBackground(Graphics g)
		{

		}

		private void changTheme()
		{
			StyleConstants.setForeground(attributeSet,
					RThemeColor.colorSet.textColor);
			document.setCharacterAttributes(0, document.getLength(),
					attributeSet, true);
		}

		@Override
		public void lightTheme()
		{
			changTheme();
		}

		@Override
		public void darkTheme()
		{
			changTheme();
		}

		@Override
		public void blackTheme()
		{
			changTheme();
		}
	}
	@Override
	public boolean needChangLanguage()
	{
		return false;
	}

	@Override
	public void setICSetting(ICSetting iSetting)
	{

	}

	@Override
	public ICSetting getICSetting()
	{
		return null;
	}

	@Override
	public void updateText(String text)
	{
	}
}
